﻿  local addon = CreateFrame"Frame"
  local _G = getfenv(0)
  local rf2_player_name, _ = UnitName("player")
  local _, rf2_player_class = UnitClass("player")
  local fontnum = "Interface\\AddOns\\eMedia\\font.ttf"
  
  -----------------------------------------------------
  -- EDIT YOUR BUFFS/DEBUFFS IN HERE
  -- IMPORTANT, TAGS HAVE TO BE UNIQUE!!!
  -----------------------------------------------------

  local testmode = 0
  
  --alpha when buff/debuff/cd not active
  --values between 0 and one are allowed
  local alpha_when_not_active = 0
  
  local alpha_when_in_combat = 0
  
  --make icon grey when buff noch active
  --0 = off // 1 = on
  local use_grey_vertex = 1
  
  local bordertexture = "Interface\\AddOns\\eMedia\\Button"
  
  if rf2_player_class == "PALADIN" then
    rf2_spell_list = {
      buffs = {
		[1] = { tag = "自由之手", spellid = 1044, unit = "player", size = 32, fontsize = 16, posx = -260, posy = 180, framestrata = "LOW", anchor = "UIParent"},
		[2] = { tag = "圣盾术", spellid = 642, unit = "target", size = 32, fontsize = 16, posx = 260, posy = 180, framestrata = "LOW", anchor = "UIParent"},
		[3] = { tag = "嗜血", spellid = 2825, unit = "player", size = 32, fontsize = 16, posx = -300, posy = 180, framestrata = "LOW", anchor = "UIParent"},
		[4] = { tag = "冰箱", spellid = 45438, unit = "target", size = 32, fontsize = 16, posx = 300, posy = 180, framestrata = "LOW", anchor = "UIParent"},
		[5] = { tag = "顺闪", spellid = 53488, unit = "player", size = 32, fontsize = 16, posx = -220, posy = 180, framestrata = "LOW", anchor = "UIParent"},
        --[2] = { tag = "commanding", spellid = 469, unit = "player", size = 32, fontsize = 24, posx = 40, posy = 0, framestrata = "BACKGROUND", anchor = "UIParent"},
      },
      debuffs = {
        --[1] = { tag = "mark",       spellid = 14325,  ismine = 0, unit = "target", size = 24, fontsize = 18, posx = -32, posy = -270, framestrata = "LOW", anchor = "UIParent"},
        --[2] = { tag = "serpent",         spellid = 49000, ismine = 0,  unit = "target", size = 24, fontsize = 18, posx = 0, posy = -270, framestrata = "LOW", anchor = "UIParent"},
        --[3] = { tag = "scorpid",         spellid = 27060, ismine = 0,  unit = "target", size = 24, fontsize = 18, posx = 32, posy = -270, framestrata = "LOW", anchor = "UIParent"},

      },
      cooldowns = {
        --[1] = { tag = "十字军打击", spellid = 35395, size = 32, fontsize = 15, posx = -40, posy = 160, framestrata = "BACKGROUND", anchor = "UIParent"},
        --[2] = { tag = "神圣风暴", spellid = 53385, size = 32, fontsize = 15, posx = 0, posy = 160, framestrata = "BACKGROUND", anchor = "UIParent"},
        --[3] = { tag = "审判", spellid = 53407, size = 32, fontsize = 15, posx = 40, posy = 160, framestrata = "BACKGROUND", anchor = "UIParent"},
		--[4] = { tag = "制裁之锤", spellid = 5588, size = 32, fontsize = 15, posx = 80, posy = 160, framestrata = "BACKGROUND", anchor = "UIParent"},
      },
    }
  elseif rf2_player_class == "DRUID" then
    rf2_spell_list = {
      buffs = {
		[1] = { tag = "兇蠻咆哮", spellid = 52610, unit = "player", size = 30, fontsize = 16, posx = 110, posy = 260, framestrata = "LOW", anchor = "UIParent"},
		[2] = { tag = "清晰預兆", spellid = 16870, unit = "player", size = 30, fontsize = 16, posx = 75, posy = 260, framestrata = "LOW", anchor = "UIParent"},
		[3] = { tag = "狂暴", spellid = 50334, unit = "player", size = 30, fontsize = 16, posx = 38, posy = 260, framestrata = "LOW", anchor = "UIParent"},
		[4] = { tag = "樹皮術", spellid = 22812, unit = "player", size = 30, fontsize = 16, posx = 0, posy = 260, framestrata = "LOW", anchor = "UIParent"},
		[5] = { tag = "求生本能", spellid = 61336, unit = "player", size = 30, fontsize = 16, posx = 110, posy = 260, framestrata = "LOW", anchor = "UIParent"},
      },
      debuffs = {
		[1] = { tag = "割碎貓", spellid = 33982, ismine = 0, unit = "target", size = 30, fontsize = 16, posx = -110, posy = 260, framestrata = "LOW", anchor = "UIParent"},
		[2] = { tag = "掃擊", spellid = 1822, ismine = 1, unit = "target", size = 30, fontsize = 16, posx = -74, posy = 260, framestrata = "LOW", anchor = "UIParent"},
		[3] = { tag = "撕扯", spellid = 1079, ismine = 1, unit = "target", size = 30, fontsize = 16, posx = -38, posy = 260, framestrata = "LOW", anchor = "UIParent"},
		[4] = { tag = "割碎熊", spellid = 33878, ismine = 0, unit = "target", size = 30, fontsize = 16, posx = -110, posy = 260, framestrata = "LOW", anchor = "UIParent"},
		[5] = { tag = "割裂熊", spellid = 33745, ismine = 1, unit = "target", size = 30, fontsize = 16, posx = -74, posy = 260, framestrata = "LOW", anchor = "UIParent"},
		[6] = { tag = "創傷", spellid = 46857, ismine = 0, unit = "target", size = 30, fontsize = 16, posx = -110, posy = 260, framestrata = "LOW", anchor = "UIParent"},
      },
      cooldowns = {
		--[1] = { tag = "樹皮", spellid = 22812, size = 30, fontsize = 16, posx = 0, posy = 360, framestrata = "LOW", anchor = "UIParent"},
      },
    }       
  else
    rf2_spell_list = {
      buffs = {

      },
      debuffs = {

      },
      cooldowns = {

      },
    }    
  end
  
  -----------------------------------------------------
  -- DO NOT TOUCH ANYTHING BELOW THIS LINE !!!
  -----------------------------------------------------
  
  addon:SetScript("OnEvent", function()
    if(event=="PLAYER_LOGIN") then
      for index,value in ipairs(rf2_spell_list.buffs) do 
        local string = rf2_spell_list.buffs[index]
        addon:rf2_create_icon(string.spellid,"rf2_"..string.tag,string.size,string.fontsize,string.posx,string.posy,string.framestrata,string.anchor)
      end
      for index,value in ipairs(rf2_spell_list.debuffs) do 
        local string = rf2_spell_list.debuffs[index]
        addon:rf2_create_icon(string.spellid,"rf2_"..string.tag,string.size,string.fontsize,string.posx,string.posy,string.framestrata,string.anchor)
      end      
      for index,value in ipairs(rf2_spell_list.cooldowns) do 
        local string = rf2_spell_list.cooldowns[index]
        addon:rf2_create_icon(string.spellid,"rf2_"..string.tag,string.size,string.fontsize,string.posx,string.posy,string.framestrata,string.anchor)
      end
      addon:rf2_onUpdate()
    end
  end)
  
  function addon:rf2_create_icon(spellId,frameName,size,fontsize,posX,posY,framestrata,anchor)
    local spellName, spellRank, SpellIcon, SpellCost, spellIsFunnel, spellPowerType, spellCastTime, spellMinRange, spellMaxRange = GetSpellInfo(spellId)
    local f = CreateFrame("Frame",frameName,UIParent)
    f:SetFrameStrata(framestrata)
    f:SetWidth(size-4)
    f:SetHeight(size-4)
    local t = f:CreateTexture(frameName.."_icon","BACKGROUND")
    t:SetTexture(SpellIcon)
    t:SetTexCoord(0.1,0.9,0.1,0.9)
    t:SetAllPoints(f)
    f.texture = t
    local t2 = f:CreateTexture(nil,"LOW")
    t2:SetTexture(bordertexture)
    t2:SetVertexColor(58/225, 102/255, 100/255)
    t2:SetPoint("TOPLEFT", f, "TOPLEFT", -4, 4)
    t2:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", 4, -4)
    f.texture = t2
    local time = f:CreateFontString(frameName.."_time", "ARTWORK")
    time:SetPoint("CENTER", f, "CENTER", 0, 1)
    time:SetFont(fontnum,fontsize,"OUTLINE")
    time:SetTextColor(1, 1, 0)
    time:SetText("")
    time:Show()
    local num = f:CreateFontString(frameName.."_num", "OVERLAY")
    num:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", 4, -4)
    num:SetFont(NAMEPLATE_FONT,floor(fontsize*0.9),"OUTLINE")
    num:SetTextColor(0.8, 0.8, 0.8)
    num:SetText("")
    num:Show()
    f:SetPoint("BOTTOM",posX,posY)
    f:Show()
	local f2 = CreateFrame("Frame", mapborder, f)
	f2:SetFrameLevel(1)
	f2:SetFrameStrata("background")
	f2:SetAlpha(0.8)
	f2:SetPoint("LEFT", f, "LEFT", -4, 0)
	f2:SetPoint("RIGHT", f, "RIGHT", 4, 0)
	f2:SetPoint("TOP", f, "TOP", 0, 4)
	f2:SetPoint("BOTTOM", f, "BOTTOM", 0, -4)
	f2:SetFrameStrata("BACKGROUND")
	f2:SetBackdrop {
	  edgeFile = "Interface\\AddOns\\eMedia\\glowTex", edgeSize = 5,
	  insets = {left = 3, right = 3, top = 3, bottom = 3}
	}
	f2:SetBackdropColor(0, 0, 0, 0)
	f2:SetBackdropBorderColor(0, 0, 0)
  end
  
  
  function addon:rf2_onUpdate()
    local f = CreateFrame("Frame", "rf2_OnUpdateFrame")
    local totalElapsed = 0
    local function rf2_OnUpdateFunc(self, elapsed)
      totalElapsed = totalElapsed + elapsed
      local rf2_update_timer = 0.1
      if (totalElapsed < rf2_update_timer) then 
        return 
      else
        totalElapsed = totalElapsed - floor(totalElapsed)
        for index,value in ipairs(rf2_spell_list.buffs) do 
          local string = rf2_spell_list.buffs[index]
          addon:rf2_check_buff(string.tag,string.spellid,string.unit)
        end
        for index,value in ipairs(rf2_spell_list.debuffs) do 
          local string = rf2_spell_list.debuffs[index]
          addon:rf2_check_debuff(string.tag,string.spellid,string.unit,string.ismine)
        end
        for index,value in ipairs(rf2_spell_list.cooldowns) do 
          local string = rf2_spell_list.cooldowns[index]
          addon:rf2_check_cooldown(string.tag,string.spellid)
        end
      end
    end
    f:SetScript("OnUpdate", rf2_OnUpdateFunc)
    f:Show()    
  end
  
 
  function addon:rf2_check_buff(frameTag,spellId,unit)
    local spellName, spellRank, SpellIcon, SpellCost, spellIsFunnel, spellPowerType, spellCastTime, spellMinRange, spellMaxRange = GetSpellInfo(spellId)
    
    if unit == nil then
      unit = "player"
    end
    
    local f = _G["rf2_"..frameTag]
    f:SetAlpha(alpha_when_not_active)
    
    if UnitAffectingCombat("player") == 1 then
      f:SetAlpha(alpha_when_in_combat)
    end
    
    local f2 = _G["rf2_"..frameTag.."_time"]
    local f3 = _G["rf2_"..frameTag.."_num"]
    f2:SetText("")
    f3:SetText("")
    
    local f4 = _G["rf2_"..frameTag.."_icon"]
    if use_grey_vertex == 1
    then
      local shaderSupported = f4:SetDesaturated(1)
    end
    
    for i = 1, 40 do
      local name, rank, texture, applications, debuffType, duration, timeleft, isMine, isStealable = UnitBuff(unit, i)
      --local name, rank, texture, applications, duration, timeleft = UnitBuff(unit, i)
      
      if name == spellName then

        timeleft = timeleft-GetTime()
        --DEFAULT_CHAT_FRAME:AddMessage("found "..name.." timeleft "..timeleft-GetTime().." duration "..duration.." now "..GetTime())

        local floortime = ""
        if timeleft ~= nil then
          if timeleft >= 60 then 
            floortime = floor((timeleft/60)+1).."m"
          elseif timeleft <= 1 then
            floortime = floor(timeleft*10)/10
          else
            floortime = floor(timeleft+0.5)  
          end
        end
        local floornum = ""
        if applications ~= nil then
          floornum = floor(applications)
          if floornum <= 1 then
            floornum = ""
          end
        end 
        
        f:SetAlpha(1)
        f2:SetText(floortime)
        f3:SetText(floornum)
        
        if use_grey_vertex == 1
        then
          local shaderSupported = f4:SetDesaturated(nil)
        end
        
      end
    end
    
    if testmode == 1 
    then
      f:SetAlpha(1)
      f2:SetText("99")
      f3:SetText("9")
    end
    
  end
  
  function addon:rf2_check_debuff(frameTag,spellId,unit,myspell)
    local spellName, spellRank, SpellIcon, SpellCost, spellIsFunnel, spellPowerType, spellCastTime, spellMinRange, spellMaxRange = GetSpellInfo(spellId)

    if unit == nil then
      unit = "target"
    end

    local f = _G["rf2_"..frameTag]
    f:SetAlpha(alpha_when_not_active)
    
    if UnitAffectingCombat("player") == 1 then
      f:SetAlpha(alpha_when_in_combat)
    end

    local f2 = _G["rf2_"..frameTag.."_time"]
    local f3 = _G["rf2_"..frameTag.."_num"]
    f2:SetText("")
    f3:SetText("")
    
    local f4 = _G["rf2_"..frameTag.."_icon"]
    if use_grey_vertex == 1
    then
      local shaderSupported = f4:SetDesaturated(1)
    end

    for i = 1, 40 do
      local name, _, texture, applications, debufftype, duration, timeleft, ismine = UnitDebuff(unit, i)
      
      if ismine == 1 or ismine == "player" then
        ismine = 1
      else
        ismine = "x"
      end
      
      if ((name == spellName and myspell == 0) or (name == spellName and myspell == ismine)) then

        timeleft = timeleft-GetTime()
        --DEFAULT_CHAT_FRAME:AddMessage("found "..name.." timeleft "..timeleft.." duration "..duration.." now "..GetTime())
      
        local floortime = ""
        if timeleft ~= nil then
          if timeleft >= 60 then 
            floortime = floor((timeleft/60)+1).."m"
          elseif timeleft <= 1 then
            floortime = floor(timeleft*10)/10
          else
            floortime = floor(timeleft+0.5)  
          end
        end
        local floornum = ""
        if applications ~= nil then
          floornum = floor(applications)
          if floornum == 0 then
            floornum = ""
          end
        end        
        
        f:SetAlpha(1)
        f2:SetText(floortime)
        f3:SetText(floornum)
        
        if use_grey_vertex == 1
        then
          local shaderSupported = f4:SetDesaturated(nil)
        end
        
      end
    end

    if testmode == 1 
    then
      f:SetAlpha(1)
      f2:SetText("99")
      f3:SetText("9")
    end
    
  end
  
  
  function addon:rf2_check_cooldown(frameTag,spellId)
    local spellName, spellRank, SpellIcon, SpellCost, spellIsFunnel, spellPowerType, spellCastTime, spellMinRange, spellMaxRange = GetSpellInfo(spellId)
    
    local f = _G["rf2_"..frameTag]
    f:SetAlpha(alpha_when_not_active)

    local f2 = _G["rf2_"..frameTag.."_time"]
    local f3 = _G["rf2_"..frameTag.."_num"]
    f2:SetText("")
    f3:SetText("")
    
    local f4 = _G["rf2_"..frameTag.."_icon"]
    if use_grey_vertex == 1
    then
      local shaderSupported = f4:SetDesaturated(1)
    end

    local spellCooldownStartTime, spellCooldownDuration, spellEnabled = GetSpellCooldown(spellName);
    
    local localstartime = 0
    if spellCooldownStartTime ~= nil then
      localstartime = spellCooldownStartTime
    end
    
    local floortime = 0
    if spellCooldownDuration ~= nil then
      floortime = spellCooldownDuration
    end
    
    local floornum = ""
    if spellEnabled ~= nil then
      floornum = floor(spellEnabled)
      if floornum == 0 then
        floornum = "1"
      else
        floornum = ""
      end
    end
    
    local now = GetTime()
    local cooldown = (localstartime+floortime-now)
    cooldown = floor(cooldown+0.5)
    
    if cooldown > 1 then
      f:SetAlpha(1)
      f2:SetText(cooldown)
      f3:SetText(floornum)
      
      if use_grey_vertex == 1
      then
        local shaderSupported = f4:SetDesaturated(nil)
      end
      
    end
      
    if testmode == 1 
    then
      f:SetAlpha(1)
      f2:SetText("99")
      f3:SetText("9")
    end

  end
  
  addon:RegisterEvent"PLAYER_LOGIN"